#ifndef __Leakage_Detect_C
#define __Leakage_Detect_C

#include "Leakage_Detect.h"

void leakageDetect_init(){
    GPIO_Config_T     configStruct;
	  #if RCD_Detect == RCD_Detect_EINT
	  EINT_Config_T     EINT_configStruct;
	  #endif
	  
    RCM_EnableAPB2PeriphClock(Leakage_RTC); 
    configStruct.mode = GPIO_MODE_IN_FLOATING; 
    configStruct.pin  = Leakage_PinId;  
    GPIO_Config(Leakage_PinPort, &configStruct);   
	  
	  #if RCD_TYPE_B > 0
	  RCM_EnableAPB2PeriphClock(Leak_TestIn_RTC);	  
	  configStruct.mode = GPIO_MODE_OUT_PP; 
    configStruct.pin  = Leak_TestIn_PinId;	  
	  GPIO_Config(Leak_TestIn_PinPort, &configStruct);	 
	  #if testSelfLeak == Leak_FM2161A 
	  leak_test_set_high(); 
	  #elif testSelfLeak == Leak_RCPD_08BS75_2P2 || testSelfLeak == UIFG_R10x_3A 
	  leak_test_set_low(); 
	  #endif
	  #endif
	  
	  #if testSelfLeak == Leak_FM2161A
	  RCM_EnableAPB2PeriphClock(Leak_CHK_RTC);	   
	  configStruct.mode = GPIO_MODE_OUT_PP;  
    configStruct.pin  = Leak_CHK_PinId;	   
	  GPIO_Config(Leak_CHK_PinPort, &configStruct); 
		leak_FMCHK_set_high();
	  #endif
				
		// 外部中断法
		#if RCD_Detect == RCD_Detect_EINT
		  RCM_EnableAPB2PeriphClock(Leakage_RCM_EINT);
			GPIO_ConfigEINTLine(Leakage_EintPort, Leakage_EintPin);
	    #ifdef AT32F403AVGT7
			// 结构体强制转换，必须有相同的字段和顺序。
	    exint_default_para_init((exint_init_type *)(&EINT_configStruct));
	    #endif
			EINT_configStruct.line    = Leakage_EintLine;	    
			EINT_configStruct.mode    = EINT_MODE_INTERRUPT; 
			EINT_configStruct.trigger = EINT_TRIGGER_RISING_FALLING;  //双边沿
			EINT_configStruct.lineCmd = ENABLE; 
			EINT_Config(&EINT_configStruct);				
		#endif
	  
}

#if RCD_Detect == RCD_Detect_POLL
uint8_t leakage_get_level(){
	  return GPIO_ReadInputBit(Leakage_PinPort, Leakage_PinId);
}
#endif

#if RCD_Detect == RCD_Detect_EINT
void leakage_EINT_start(){
	  NVIC_EnableIRQRequest(Leakage_Eint_IRQn, 0, 0);
	  EINT_ClearIntFlag(Leakage_EintLine);
}
#endif

#ifndef UsingFreeRTOS
#if (RCD_TYPE_B > 0) && (testSelfLeak > 0)
//B型互感器自检步骤
#if testSelfLeak == Leak_RCPD_08BS75_2P2 || testSelfLeak == UIFG_R10x_3A 
void leak_test_step_run(){
	  #if RTC_ON > 0
		getTimestampRTC();
		#endif
    if(system_time - leakData.leakSelfTest_start_tm > leak_step1_tm && leakData.leak_test_init_step == 1){
				leak_test_set_high();
				leakData.leak_test_init_step = 2;
			  leakData.leak_testSelf_occs = 0;
		}else if(system_time - leakData.leakSelfTest_start_tm > leak_step2_tm && leakData.leak_test_init_step == 2){
			  leak_test_set_low();
				leakData.leak_test_init_step = 3;
		#if leak_testSelf_get > 0		
		}else if(system_time - leakData.leakSelfTest_start_tm > leak_step3_tm && leakData.leak_test_init_step == 3 && leakData.leak_testSelf_occs > 1){
		#else
		}else if(system_time - leakData.leakSelfTest_start_tm > leak_step3_tm && leakData.leak_test_init_step == 3){			
		#endif
				leakData.leak_test_init_step = 4;
			  leakData.leak_tms = 0;
		} else if(system_time - leakData.leakSelfTest_start_tm > leak_step4_tm && leakData.leak_test_init_step == 4 && leakage_get_level() == 0){
				leakData.leak_test_init_step = 5;
			  leakData.leak_tms = 0;
		} 
}

#elif testSelfLeak == Leak_FM2161A
void leak_test_step_run(){
	  #if RTC_ON > 0
		getTimestampRTC();
		#endif
    if(system_time - leakData.leakSelfTest_start_tm > Leak_step_t1 && leakData.leak_test_init_step == 1){
				leak_FMCHK_set_low();
				leakData.leak_test_init_step = 2;			  
		}else if(system_time - leakData.leakSelfTest_start_tm > Leak_step_t2 && leakData.leak_test_init_step == 2){
			  leak_FMCHK_set_high();
				leakData.leak_test_init_step = 3;
		}else if(system_time - leakData.leakSelfTest_start_tm > Leak_step_t3 && leakData.leak_test_init_step == 3){
				leak_test_set_low();			  
				leakData.leak_test_init_step = 4;
			  leakData.leak_testSelf_occs = 0;
		}else if(system_time - leakData.leakSelfTest_start_tm > Leak_step_t4 && leakData.leak_test_init_step == 4){
			  leak_test_set_high();
				leakData.leak_test_init_step = 5;
		#if leak_testSelf_get > 0	
		}else if(system_time - leakData.leakSelfTest_start_tm > Leak_step_t5 && leakData.leak_test_init_step == 5 && leakData.leak_testSelf_occs > 1){
		#else
		}else if(system_time - leakData.leakSelfTest_start_tm > Leak_step_t5 && leakData.leak_test_init_step == 5){	//不获取自检信号
		#endif
  			leakData.leak_test_init_step = 6;
			  leakData.leak_tms = 0;
		}else if(system_time - leakData.leakSelfTest_start_tm > leak_stepLast_tm && leakData.leak_test_init_step == 6  && leakage_get_level() == 0){
			  leakData.leak_test_init_step = 7;
			  leakData.leak_tms = 0;
		}
		
}
#endif
#endif
#endif //#ifndef UsingFreeRTOS

#endif



